/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.examenes.vista;

import basedatos.OperacionesBD;
import java.io.File;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import modulos.consulta.modelo.Consulta;
import modulos.examenes.modelo.ExamenImagen;
import vista.modelo.CopiaArchivos;
import vista.modelo.ImagePreviewPanel;
import vista.modelo.OperacionesVarias;
import vista.modelo.PonerImagenLabel;

/**
 *
 * @author Edulima
 */
public class iFrmExamenesImagen1 extends javax.swing.JInternalFrame {

    /**
     * Creates new form iFrmExamenesImagen1
     */
    private FileNameExtensionFilter filtroImagen;
    private File dirImagen1;
    private File dirImagen2;
    private File dirImagen3;
    private ImageIcon icono;
    private Consulta consulta = new Consulta();
    private ExamenImagen examenImagen = new ExamenImagen();
    private int accion = -1;
    public static final int ACCION_NUEVO = 10;
    public static final int ACCION_MODIFICAR = 11;

    public iFrmExamenesImagen1(Consulta consulta) {
        initComponents();
        OperacionesVarias.cerrarVentanaEscape(this);
        this.setVisible(true);
        this.consulta = consulta;
        accion = ACCION_NUEVO;
    }

    public iFrmExamenesImagen1(ExamenImagen examenImagen) {
        initComponents();
        OperacionesVarias.cerrarVentanaEscape(this);
        this.setVisible(true);
        this.examenImagen = examenImagen;
        this.consulta = examenImagen.getConsulta();
        accion = ACCION_MODIFICAR;
        vistaPreviaImagenes(examenImagen);
    }

    private void vistaPreviaImagenes(ExamenImagen examenImagen) {
        ArrayList<String> archivos = new ArrayList<String>();
        archivos.add(examenImagen.getImagen1());
        archivos.add(examenImagen.getImagen2());
        archivos.add(examenImagen.getImagen3());
        int cont = 0;

        for (String path : archivos) {
            cont++;
            File f = new File(path);
            switch (cont) {
                case 1:
                    icono = new ImageIcon(path);
                    lblImagen1.setIcon(PonerImagenLabel.loadImage(icono.getImage()));
                    dirImagen1 = f;
                    break;
                case 2:
                    icono = new ImageIcon(path);
                    lblImagen2.setIcon(PonerImagenLabel.loadImage(icono.getImage()));
                    dirImagen2 = f;
                    break;
                case 3:
                    icono = new ImageIcon(path);
                    lblImagen3.setIcon(PonerImagenLabel.loadImage(icono.getImage()));
                    dirImagen3 = f;
                    break;
            }
        }
        txtExamen.setText(examenImagen.getNombreExamen());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnCargarImagenPrincipal = new javax.swing.JButton();
        lblImagen1 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtExamen = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        btnCargarImagenPrincipal1 = new javax.swing.JButton();
        lblImagen2 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btnCargarImagenPrincipal2 = new javax.swing.JButton();
        lblImagen3 = new javax.swing.JLabel();

        setTitle("EXAMENES DE IMAGEN");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/imagen.png"))); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Seleccione la imagen del Examen", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Dialog", 1, 12), java.awt.SystemColor.activeCaption)); // NOI18N
        jPanel1.setLayout(null);

        btnCargarImagenPrincipal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/camera.png"))); // NOI18N
        btnCargarImagenPrincipal.setText("Cargar");
        btnCargarImagenPrincipal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarImagenPrincipalActionPerformed(evt);
            }
        });
        jPanel1.add(btnCargarImagenPrincipal);
        btnCargarImagenPrincipal.setBounds(55, 66, 129, 31);
        jPanel1.add(lblImagen1);
        lblImagen1.setBounds(275, 13, 120, 80);

        jLabel1.setText("Examen:");

        txtExamen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtExamenActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/filesave.png"))); // NOI18N
        jButton1.setText("Guardar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        jButton2.setText("Cerrar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Seleccione la imagen del Examen", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Dialog", 1, 12), java.awt.SystemColor.activeCaption)); // NOI18N
        jPanel2.setLayout(null);

        btnCargarImagenPrincipal1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/camera.png"))); // NOI18N
        btnCargarImagenPrincipal1.setText("Cargar");
        btnCargarImagenPrincipal1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarImagenPrincipal1ActionPerformed(evt);
            }
        });
        jPanel2.add(btnCargarImagenPrincipal1);
        btnCargarImagenPrincipal1.setBounds(55, 66, 129, 31);
        jPanel2.add(lblImagen2);
        lblImagen2.setBounds(275, 13, 120, 80);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Seleccione la imagen del Examen", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Dialog", 1, 12), java.awt.SystemColor.activeCaption)); // NOI18N
        jPanel3.setLayout(null);

        btnCargarImagenPrincipal2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/camera.png"))); // NOI18N
        btnCargarImagenPrincipal2.setText("Cargar");
        btnCargarImagenPrincipal2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarImagenPrincipal2ActionPerformed(evt);
            }
        });
        jPanel3.add(btnCargarImagenPrincipal2);
        btnCargarImagenPrincipal2.setBounds(55, 66, 129, 31);
        jPanel3.add(lblImagen3);
        lblImagen3.setBounds(275, 13, 120, 80);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(txtExamen, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 24, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 532, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 532, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 532, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(152, 152, 152))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtExamen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(37, 37, 37))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCargarImagenPrincipalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarImagenPrincipalActionPerformed
        JFileChooser jf = new JFileChooser();
        ImagePreviewPanel miniatura = new ImagePreviewPanel();
        jf.setAccessory(miniatura);
        jf.addPropertyChangeListener(miniatura);
        jf.setFileFilter(filtroImagen);
        int opc = jf.showOpenDialog(this);
        if (opc == JFileChooser.APPROVE_OPTION) {
            dirImagen1 = jf.getSelectedFile();
            icono = new ImageIcon(dirImagen1.toString());
            lblImagen1.setIcon(PonerImagenLabel.loadImage(icono.getImage()));
        }

    }//GEN-LAST:event_btnCargarImagenPrincipalActionPerformed

    private void btnCargarImagenPrincipal1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarImagenPrincipal1ActionPerformed
        JFileChooser jf = new JFileChooser();
        ImagePreviewPanel miniatura = new ImagePreviewPanel();
        jf.setAccessory(miniatura);
        jf.addPropertyChangeListener(miniatura);
        jf.setFileFilter(filtroImagen);
        int opc = jf.showOpenDialog(this);
        if (opc == JFileChooser.APPROVE_OPTION) {
            dirImagen2 = jf.getSelectedFile();
            icono = new ImageIcon(dirImagen2.toString());
            lblImagen2.setIcon(PonerImagenLabel.loadImage(icono.getImage()));
        }
    }//GEN-LAST:event_btnCargarImagenPrincipal1ActionPerformed

    private void btnCargarImagenPrincipal2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarImagenPrincipal2ActionPerformed
        JFileChooser jf = new JFileChooser();
        ImagePreviewPanel miniatura = new ImagePreviewPanel();
        jf.setAccessory(miniatura);
        jf.addPropertyChangeListener(miniatura);
        jf.setFileFilter(filtroImagen);
        int opc = jf.showOpenDialog(this);
        if (opc == JFileChooser.APPROVE_OPTION) {
            dirImagen3 = jf.getSelectedFile();
            icono = new ImageIcon(dirImagen3.toString());
            lblImagen3.setIcon(PonerImagenLabel.loadImage(icono.getImage()));
        }
    }//GEN-LAST:event_btnCargarImagenPrincipal2ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (!txtExamen.getText().isEmpty()) {
            if (dirImagen1 != null || dirImagen2 != null || dirImagen3 != null) {
                ArrayList<File> archivos = new ArrayList<File>();
                archivos.add(dirImagen1);
                archivos.add(dirImagen2);
                archivos.add(dirImagen3);
                CopiaArchivos c=new CopiaArchivos();
                File directorio = new File(c.directorioBase() + "/" + consulta.getUsuario().getNombre() + " "
                        + consulta.getUsuario().getApellido() + " " + consulta.getUsuario().getEspecialidad() + "/"
                        + consulta.getPaciente().getNumeroHC() + "/" + consulta.getNumConsul() + "/" + txtExamen.getText());
                directorio.mkdir();
                directorio.mkdirs();


                int cont = 0;
                for (File archivo : archivos) {
                    if (archivo != null) {
                        cont++;
                        try {
                            String path = directorio.getAbsolutePath() + "/Imagen" + cont + ".png";
                            if (!path.equals(archivo.getAbsolutePath())) {
                                CopiaArchivos.Copiar(archivo.getAbsolutePath(), path);
                                switch (cont) {
                                    case 1:
                                        examenImagen.setImagen1(path);
                                        break;
                                    case 2:
                                        examenImagen.setImagen2(path);
                                        break;
                                    case 3:
                                        examenImagen.setImagen3(path);
                                        break;
                                }
                            }
                        } catch (Exception ex) {
                            Logger.getLogger(iFrmExamenesImagen1.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
                examenImagen.setNombreExamen(txtExamen.getText());
                examenImagen.setConsulta(consulta);
                if (accion == ACCION_NUEVO) {
                    if (OperacionesBD.guardar(examenImagen)) {
                        JOptionPane.showMessageDialog(this, "Examen guardado correctamente", "Guardar Examen", JOptionPane.INFORMATION_MESSAGE);
                        iFrmExamenesImagen.listarExamenes();
                        dispose();
                    } else {
                        JOptionPane.showMessageDialog(this, "No se ha podido guardar el examen", "Guardar Examen", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    if (OperacionesBD.modificar(examenImagen)) {
                        JOptionPane.showMessageDialog(this, "Examen actualizado correctamente", "Actualizar Examen", JOptionPane.INFORMATION_MESSAGE);
                        iFrmExamenesImagen.listarExamenes();
                        dispose();
                    } else {
                        JOptionPane.showMessageDialog(this, "No se ha podido actualizar el examen", "Actualizar Examen", JOptionPane.ERROR_MESSAGE);
                    }
                }
            } else {
                JOptionPane.showMessageDialog(this, "Debe cargar almenos una imagen del examen", "Guardar Examen", JOptionPane.WARNING_MESSAGE);

            }
        } else {
            JOptionPane.showMessageDialog(this, "Antes de guardar ingrese el nombre del examen", "Guardar Examen", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void txtExamenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtExamenActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtExamenActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCargarImagenPrincipal;
    private javax.swing.JButton btnCargarImagenPrincipal1;
    private javax.swing.JButton btnCargarImagenPrincipal2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lblImagen1;
    private javax.swing.JLabel lblImagen2;
    private javax.swing.JLabel lblImagen3;
    private javax.swing.JTextField txtExamen;
    // End of variables declaration//GEN-END:variables
}
